﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppMain
{
    public partial class PreviewSwitchCfg : Form
    {
        public byte _numberOfAlternates = 1;

        RadioButton[] _alternate = new RadioButton[16];
        
        public PreviewSwitchCfg(byte numberOfAlternates)
        {
            InitializeComponent();

            _numberOfAlternates = numberOfAlternates;
            // Create Radio Buttons
            for (byte x = 0; x < _numberOfAlternates; x++)
            {
                if (x >= 16)
                {
                    break;
                }
                _alternate[x] = new RadioButton();
                _alternate[x].AutoSize = true;
                _alternate[x].Location = new System.Drawing.Point(7 + ((x * 630) / _numberOfAlternates), 443);
                _alternate[x].Name = "Alternate" + x.ToString();
                _alternate[x].Size = new System.Drawing.Size(31, 17);
                _alternate[x].TabIndex = x + 1;
                _alternate[x].TabStop = true;
                _alternate[x].Text = (x+1).ToString();
                _alternate[x].UseVisualStyleBackColor = true;
                _alternate[x].Click += new System.EventHandler(SelectAlternate);
                this.Controls.Add(_alternate[x]);
            }
            if (_alternate[0] != null)
            {
                _alternate[0].Checked = true;
            }
        }

        public void RedisplayRadioButtons(byte numberOfAlternates)
        {
            _numberOfAlternates = numberOfAlternates;
            // Modify Radio Buttons
            for (byte x = 0; x < 16; x++)
            {
                if (_alternate[x] != null)
                {
                    if (_alternate[x].Visible)
                    {
                        _alternate[x].Visible = false;
                        this.Controls.Remove(_alternate[x]);
                    }
                }
                if (x < _numberOfAlternates)
                {
                    _alternate[x] = new RadioButton();
                    _alternate[x].AutoSize = true;
                    _alternate[x].Location = new System.Drawing.Point(7 + ((x * 650) / _numberOfAlternates), 438);
                    _alternate[x].Name = "Alternate" + x.ToString();
                    _alternate[x].Size = new System.Drawing.Size(31, 17);
                    _alternate[x].TabIndex = x + 1;
                    _alternate[x].TabStop = true;
                    _alternate[x].Text = (x + 1).ToString();
                    _alternate[x].UseVisualStyleBackColor = true;
                    _alternate[x].Click += new System.EventHandler(SelectAlternate);
                    this.Controls.Add(_alternate[x]);
                }
                if (_alternate[0] != null)
                {
                    _alternate[0].Checked = true;
                }
            }
        }

        private void SelectAlternate(object sender, EventArgs e)
        {
            byte alternate = 0;
            for (byte x = 0; x < _numberOfAlternates; x++)
            {
                if (_alternate[x] != null)
                {
                    if (_alternate[x].Checked)
                    {
                        alternate = x;
                    }
                }
                else
                {
                    alternate = 0;
                }
            }
            bool valid = SwMatrix.UpdateWorkingSwitchConfigWithAlternate(alternate);

            if(valid)
            {
                SwMatrix._lastAlternate = alternate;
                byte[] workingTargetSwitchPositions = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
                for (byte x = 0; x < SwMatrix.NumberOfSupportedSwitches; x++)
                {
                    if (SwMatrix._workingTargetSwitchPositions[x])
                    {
                        workingTargetSwitchPositions[x] = 1;
                    }
                    else
                    {
                        workingTargetSwitchPositions[x] = 0;
                    }

                }
                SwMatrix.CalculateTransmitterFeeds(workingTargetSwitchPositions, SwMatrix._switchPositions);
                // do nothing at all, since we can't seem to display a messagebox.
            }
        }
        
        private void SwitchMatrixTimer_Tick(object sender, EventArgs e)
        {
            Transmitter6.BackColor = SwMatrix._txColor[6];
            if (SwMatrix._inhibitedTransmitters[1])
            {
                Transmitter6.BorderStyle = BorderStyle.FixedSingle;
                Transmitter6.ForeColor = Color.Orange;
            }
            else
            {
                Transmitter6.BorderStyle = BorderStyle.Fixed3D;
                Transmitter6.ForeColor = Color.Black;
            }
            Transmitter8.BackColor = SwMatrix._txColor[8];
            if (SwMatrix._inhibitedTransmitters[3])
            {
                Transmitter8.BorderStyle = BorderStyle.FixedSingle;
                Transmitter8.ForeColor = Color.Orange;
            }
            else
            {
                Transmitter8.BorderStyle = BorderStyle.Fixed3D;
                Transmitter8.ForeColor = Color.Black;
            }
            Transmitter7.BackColor = SwMatrix._txColor[7];
            if (SwMatrix._inhibitedTransmitters[2])
            {
                Transmitter7.BorderStyle = BorderStyle.FixedSingle;
                Transmitter7.ForeColor = Color.Orange;
            }
            else
            {
                Transmitter7.BorderStyle = BorderStyle.Fixed3D;
                Transmitter7.ForeColor = Color.Black;
            }
            Transmitter5.BackColor = SwMatrix._txColor[5];
            if (SwMatrix._inhibitedTransmitters[0])
            {
                Transmitter5.BorderStyle = BorderStyle.FixedSingle;
                Transmitter5.ForeColor = Color.Orange;
            }
            else
            {
                Transmitter5.BorderStyle = BorderStyle.Fixed3D;
                Transmitter5.ForeColor = Color.Black;
            }
            TX6toSW2_1.BackColor = SwMatrix._txColor[6];
            TX6toSW2_2.BackColor = SwMatrix._txColor[6];
            TX8toSW2_1.BackColor = SwMatrix._txColor[8];
            TX8toSW2_2.BackColor = SwMatrix._txColor[8];
            TX8toSW2_3.BackColor = SwMatrix._txColor[8];
            TX5toSW1_1.BackColor = SwMatrix._txColor[5];
            TX7toSW1_1.BackColor = SwMatrix._txColor[7];
            TX7toSW1_2.BackColor = SwMatrix._txColor[7];
            // This is calculated elsewhere
            //SwMatrix.CalculateTransmitterFeeds(SwMatrix._switchPositions);
            // track current Switch (makes copy/paste easier)
            byte currentSwitch = 0;
            // Switch 1 Outputs
            SW1toSW5_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW1toSW5_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW1toSW3_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch1.ImageLocation = SwMatrix._imageLocations[0];
            Switch1.Load();
            if (Switch1.Tag != null)
            {
                if (Switch1.Tag.ToString() == "90")
                {
                    Switch1.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                }
                if (Switch1.Tag.ToString() == "180")
                {
                    Switch1.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                }
                if (Switch1.Tag.ToString() == "270")
                {
                    Switch1.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                }
            }
            // Switch 2 Outputs
            currentSwitch++;
            SW2toSW4_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW2toSW3_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch2.ImageLocation = SwMatrix._imageLocations[1];
            Switch2.Load();
            if (Switch2.Tag != null)
            {
                if (Switch2.Tag.ToString() == "90")
                {
                    Switch2.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                }
                if (Switch2.Tag.ToString() == "180")
                {
                    Switch2.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                }
                if (Switch2.Tag.ToString() == "270")
                {
                    Switch2.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                }
            }
            // Switch 3 Outputs
            currentSwitch++;
            SW3toSW4_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW3toSW4_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW3toSW4_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW3toSW7_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch3.ImageLocation = SwMatrix._imageLocations[2];
            Switch3.Load();
            if (Switch3.Tag != null)
            {
                if (Switch3.Tag.ToString() == "90")
                {
                    Switch3.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                }
                if (Switch3.Tag.ToString() == "180")
                {
                    Switch3.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                }
                if (Switch3.Tag.ToString() == "270")
                {
                    Switch3.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                }
            }
            // Switch 4 Outputs
            currentSwitch++;
            SW4toSW5_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW4toSW5_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW4toSW5_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW4toSW5_4.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW4toSW7_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch4.ImageLocation = SwMatrix._imageLocations[3];
            Switch4.Load();
            if (Switch4.Tag != null)
            {
                if (Switch4.Tag.ToString() == "90")
                {
                    Switch4.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                }
                if (Switch4.Tag.ToString() == "180")
                {
                    Switch4.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                }
                if (Switch4.Tag.ToString() == "270")
                {
                    Switch4.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                }
            }
            // Switch 5 Outputs
            currentSwitch++;
            SW5toSW8_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            SW5toSW6_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW5toSW6_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            Switch5.ImageLocation = SwMatrix._imageLocations[4];
            Switch5.Load();
            if (Switch5.Tag != null)
            {
                if (Switch5.Tag.ToString() == "90")
                {
                    Switch5.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                }
                if (Switch5.Tag.ToString() == "180")
                {
                    Switch5.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                }
                if (Switch5.Tag.ToString() == "270")
                {
                    Switch5.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                }
            }
            // Switch 6 Outputs
            currentSwitch++;
            SW6toANT4_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW6toANT4_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW6toANT1_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch6.ImageLocation = SwMatrix._imageLocations[5];
            Switch6.Load();
            if (Switch6.Tag != null)
            {
                if (Switch6.Tag.ToString() == "90")
                {
                    Switch6.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                }
                if (Switch6.Tag.ToString() == "180")
                {
                    Switch6.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                }
                if (Switch6.Tag.ToString() == "270")
                {
                    Switch6.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                }
            }
            // Switch 7 Outputs
            currentSwitch++;
            SW7toSW6_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW7toSW6_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW7toSW6_3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW7toSW10_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch7.ImageLocation = SwMatrix._imageLocations[6];
            Switch7.Load();
            if (Switch7.Tag != null)
            {
                if (Switch7.Tag.ToString() == "90")
                {
                    Switch7.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                }
                if (Switch7.Tag.ToString() == "180")
                {
                    Switch7.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                }
                if (Switch7.Tag.ToString() == "270")
                {
                    Switch7.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                }
            }
            // Switch 8 Outputs
            currentSwitch++;
            SW8toANT2B_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW8toANT2B_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW8toANT2A_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch8.ImageLocation = SwMatrix._imageLocations[7];
            Switch8.Load();
            if (Switch8.Tag != null)
            {
                if (Switch8.Tag.ToString() == "90")
                {
                    Switch8.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                }
                if (Switch8.Tag.ToString() == "180")
                {
                    Switch8.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                }
                if (Switch8.Tag.ToString() == "270")
                {
                    Switch8.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                }
            }
            // Switch 9 Outputs
            currentSwitch++;
            SW9toLOAD_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW9toLOAD_2.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW9toANT3_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch9.ImageLocation = SwMatrix._imageLocations[8];
            Switch9.Load();
            if (Switch9.Tag != null)
            {
                if (Switch9.Tag.ToString() == "90")
                {
                    Switch9.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                }
                if (Switch9.Tag.ToString() == "180")
                {
                    Switch9.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                }
                if (Switch9.Tag.ToString() == "270")
                {
                    Switch9.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                }
            }
            // Switch 10 Outputs
            currentSwitch++;
            SW10toANT5_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 0)];
            SW10toSW9_1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingSegment(currentSwitch, 1)];
            Switch10.ImageLocation = SwMatrix._imageLocations[9];
            Switch10.Load();
            if (Switch10.Tag != null)
            {
                if (Switch10.Tag.ToString() == "90")
                {
                    Switch10.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
                }
                if (Switch10.Tag.ToString() == "180")
                {
                    Switch10.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone);
                }
                if (Switch10.Tag.ToString() == "270")
                {
                    Switch10.Image.RotateFlip(System.Drawing.RotateFlipType.Rotate270FlipNone);
                }
            }
            // Antennas
            Antenna1.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingAntenna(0)];
            Antenna2A.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingAntenna(1)];
            Antenna2B.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingAntenna(2)];
            Antenna3.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingAntenna(3)];
            Antenna4.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingAntenna(4)];
            Antenna5.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingAntenna(5)];
            DummyLoad.BackColor = SwMatrix._txColor[SwMatrix.TransmitterFeedingAntenna(6)];
            if (SwMatrix._unreadyAntennas[0])
            {
                Antenna1.BorderStyle = BorderStyle.FixedSingle;
                Antenna1.ForeColor = Color.Orange;
            }
            else
            {
                Antenna1.BorderStyle = BorderStyle.Fixed3D;
                Antenna1.ForeColor = Color.White;
            }
            if (SwMatrix._unreadyAntennas[1])
            {
                Antenna2A.BorderStyle = BorderStyle.FixedSingle;
                Antenna2A.ForeColor = Color.Orange;
            }
            else
            {
                Antenna2A.BorderStyle = BorderStyle.Fixed3D;
                Antenna2A.ForeColor = Color.White;
            }
            if (SwMatrix._unreadyAntennas[2])
            {
                Antenna2B.BorderStyle = BorderStyle.FixedSingle;
                Antenna2B.ForeColor = Color.Orange;
            }
            else
            {
                Antenna2B.BorderStyle = BorderStyle.Fixed3D;
                Antenna2B.ForeColor = Color.White;
            }
            if (SwMatrix._unreadyAntennas[3])
            {
                Antenna3.BorderStyle = BorderStyle.FixedSingle;
                Antenna3.ForeColor = Color.Orange;
            }
            else
            {
                Antenna3.BorderStyle = BorderStyle.Fixed3D;
                Antenna3.ForeColor = Color.White;
            }
            if (SwMatrix._unreadyAntennas[4])
            {
                Antenna4.BorderStyle = BorderStyle.FixedSingle;
                Antenna4.ForeColor = Color.Orange;
            }
            else
            {
                Antenna4.BorderStyle = BorderStyle.Fixed3D;
                Antenna4.ForeColor = Color.White;
            }
            if (SwMatrix._unreadyAntennas[5])
            {
                Antenna5.BorderStyle = BorderStyle.FixedSingle;
                Antenna5.ForeColor = Color.Orange;
            }
            else
            {
                Antenna5.BorderStyle = BorderStyle.Fixed3D;
                Antenna5.ForeColor = Color.White;
            }
            if (SwMatrix._unreadyAntennas[6])
            {
                DummyLoad.BorderStyle = BorderStyle.FixedSingle;
                DummyLoad.ForeColor = Color.Orange;
            }
            else
            {
                DummyLoad.BorderStyle = BorderStyle.Fixed3D;
                DummyLoad.ForeColor = Color.White;
            }
            Fault1.Visible = SwMatrix._switchError[0];
            Fault2.Visible = SwMatrix._switchError[1];
            Fault3.Visible = SwMatrix._switchError[2];
            Fault4.Visible = SwMatrix._switchError[3];
            Fault5.Visible = SwMatrix._switchError[4];
            Fault6.Visible = SwMatrix._switchError[5];
            Fault7.Visible = SwMatrix._switchError[6];
            Fault8.Visible = SwMatrix._switchError[7];
            Fault9.Visible = SwMatrix._switchError[8];
            Fault10.Visible = SwMatrix._switchError[9];
            
            TX5_Locked.Visible = SwMatrix._transmitterLocked[5];
            TX6_Locked.Visible = SwMatrix._transmitterLocked[6];
            TX7_Locked.Visible = SwMatrix._transmitterLocked[7];
            TX8_Locked.Visible = SwMatrix._transmitterLocked[8];

            ConfigCost.Text = SwMatrix._configCost.ToString();
        }
    }
}
